<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/flash/getpagedata.php
#	FUNCTION:		Gets the content of a page or newsletter in XML format
#	AUTHOR:			Cameron Morrow
#	CREATED:		26/07/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Report all errors
error_reporting(E_ALL);

# Start session
session_start();

# Use this for IE to remember form data correctly
header("Cache-control: private");

# Vars
$ERROR_ARRAY = array();
$SQL_ARRAY = array();

# Includes
include "../../site_specific/project.php";
include "../../includes/constants.php";
include "../../includes/utilities.php";
include "../../includes/pages.php";
include "../../includes/database.php";
include "../../includes/users.php";
include "../../includes/files.php";
include "../../includes/date.php";
include "../../includes/text.php";

# Establish database connection
establishDatabaseConnection();

# Page (or newsletter) ID
$PAGE_ID = (@$_POST["page"]) ? $_POST["page"] : "index";
$PAGE_ID = (@$_GET["page"]) ? $_GET["page"] : $PAGE_ID;
#$PAGE_ID = (@$_POST["page"]) ? $_POST["page"] : @$_GET["page"] ? $_GET["page"] : "index";

# Type
$TYPE = (@$_POST["type"]) ? $_POST["type"] : "page";

# Get page or newsletter
if ($TYPE == "page") {
	$sql = "SELECT p_name, p_content FROM " . $PROJECT_DB_TABLES["pages"] . " WHERE p_id = '" . $PAGE_ID . "'";
	$page = getRecord($sql);
} else {
	$sql = "SELECT n_name, n_content FROM " . $PROJECT_DB_TABLES["newsletters"] . " WHERE n_id = " . $PAGE_ID;
	$page = getRecord($sql);
}

# If page content was retrieved
if (count($page) > 0) {

	# Parse content
	$page_content = ($TYPE == "page") ? $page[0]["p_content"] : $page[0]["n_content"];
	$page_content = str_replace("@", "%", $page_content);

	# Name
	$page_name = ($TYPE == "page") ? $page[0]["p_name"] : $page[0]["n_name"];


	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# P A R S E   F O R   N O N - E N C O D E D   T E X T
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	# Check if page is Old School by re-parseing text
	$page_content = explode("<text display=", $page_content);
	foreach ($page_content as $pc => $content) {
		if ($pc > 0) {

			# Extract Elements
			$prefix = substr($content, 0, strpos($content, ">") + 1);
			$decode = substr($content, strlen($prefix));
			$decode = substr($decode, 0, strpos($decode, "</text>"));
			$suffix = substr($content, strlen($prefix) + strlen($decode));

			# Escape chars
			$decode = flash_escape(flash_unescape($decode));

			# Recompile elements
			$page_content[$pc] = $prefix . $decode . $suffix;
		}
	}
	$page_content = implode("<text display=", $page_content);


	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# P A R S E   F O R   N O N - E N C O D E D   H T M L
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	# Then check HTML
	$page_content = explode("<HTMLTextbox display=", $page_content);
	foreach ($page_content as $pc => $content) {
		if ($pc > 0) {

			# Extract Elements
			$prefix = substr($content, 0, strpos($content, ">") + 1);
			$decode = substr($content, strlen($prefix));
			$decode = substr($decode, 0, strpos($decode, "</HTMLTextbox>"));
			$suffix = substr($content, strlen($prefix) + strlen($decode));

			# Escape chars
			$decode = flash_escape(flash_unescape($decode));

			# Recompile elements
			$page_content[$pc] = $prefix . $decode . $suffix;
		}
	}
	$page_content = implode("<HTMLTextbox display=", $page_content);


	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	# C H A N G E    C O D E   F O R   S E N D I N G   T O   F L A S H
	# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	# Flash will automatically try to interpret URL encoded items, so throw it off by changing the % to a @;
	$page_content = str_replace("%", "@", $page_content);

	#$save = createFile("../", "load-log-" . getFullDate() . "-" . rand(100, 999) . ".txt", $page_content);

	# Echo result out
	echo "loadsuccess=true&title=" . $page_name . "&content=" . $page_content;

} else {

	echo "loadsuccess=true&title=error&content=";
}

?>